package com.java.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.java.entity.AccountInfo;
import com.java.entity.event.AccountInfoEvent;
import com.java.mapper.AccountInfoMapper;
import com.java.service.AccountService;
import com.java.util.MyCons;
import com.java.util.MyMap;
import com.java.util.MyTools;
import com.java.util.resp.BaseRespone;
import com.java.util.resp.TableRespone;
import com.java.util.servlet.ServletUtil;

@Service
public class AccountServiceImpl implements AccountService {
	
	@Autowired private AccountInfoMapper accountMapper;

	@Override
	public BaseRespone saveAuth(String openId, String nick, String logo) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public BaseRespone login(String user, String password) {
		AccountInfoEvent info = accountMapper.selectByInfo(new AccountInfo().setAccountUser(user));
		if(null == info) {
			return BaseRespone.error("该账号不存在");
		}
		if(info.getAccountStatus() == 0) {
			return BaseRespone.error("该账号未生效");
		}
		if(info.getAccountStatus() == 2) {
			return BaseRespone.error("您已被拉入黑名单");
		}
		if(!info.getAccountPassword().equals(password)) {
			return BaseRespone.error("密码错误");
		}
		ServletUtil.getSession().setAttribute(MyCons.USER_ROLE, info);
		return BaseRespone.success("登陆成功");
	}

	@Override
	public BaseRespone register(String userName, String password, String nick) {
		AccountInfoEvent info = accountMapper.selectByInfo(new AccountInfo().setAccountUser(userName));
		if(null != info) {
			return BaseRespone.error("该账号已被注册");
		}
		AccountInfo save = new AccountInfo();
		save.setAccountId(MyTools.getUUID())
		.setAccountUser(userName)
		.setAccountNick(nick)
		.setAccountPassword(password)
		.setAccountTime(new Date())
		.setAccountStatus(1);
		int row = accountMapper.insertSelective(save);
		if(row == 0) {
			return BaseRespone.error("注册失败");
		}
		return BaseRespone.success("注册成功，请登录");
	}

	@Override
	public AccountInfoEvent getAccount(String accountId) {
		return accountMapper.selectByKey(accountId);
	}

	@Override
	public TableRespone getAccountList(int page, int size) {
		MyMap mm = new MyMap();
		int offset = TableRespone.getOffset(page, size);
		List<AccountInfoEvent> list = accountMapper.selectAll(mm, offset, size);
		List<AccountInfoEvent> count = accountMapper.selectAll(mm, 0, 0);
		return new TableRespone(list, count.size());
	}

	@Override
	public BaseRespone update(AccountInfo account) {
		AccountInfo info = accountMapper.selectByKey(account.getAccountId());
		info.setAccountNick(account.getAccountNick())
		.setAccountMobile(account.getAccountMobile())
		.setAccountLogo(account.getAccountLogo());
		if(!MyTools.isEmpty(account.getAccountPassword())) {
			info.setAccountPassword(account.getAccountPassword());
		}
		int row = accountMapper.updateSelective(info);
		if(row == 0) {
			return BaseRespone.error("保存失败");
		}
		return BaseRespone.success("修改成功");
	}

	@Override
	public BaseRespone updateRole(String accountId) {
		AccountInfo info = accountMapper.selectByKey(accountId);
		info.setAccountRole(1);
		int row = accountMapper.updateSelective(info);
		if(row == 0) {
			return BaseRespone.error("设置失败");
		}
		return BaseRespone.success("已设置");
	}

	@Override
	public BaseRespone updateUser(AccountInfo info) {
		int row = 0;
		AccountInfo has = accountMapper.selectByKey(info.getAccountId());
		AccountInfoEvent checkInfo = accountMapper.selectByInfo(new AccountInfo().setAccountUser(info.getAccountUser()));
		if(null == has) {
			if(null != checkInfo) {
				return BaseRespone.error("该账号已存在");
			}
			has = new AccountInfo();
			has.setAccountId(MyTools.getUUID());
			has.setAccountTime(new Date());
			row = accountMapper.insertSelective(has);
		}else {
			if(!has.getAccountUser().equals(checkInfo.getAccountUser())) {
				return BaseRespone.error("该账号已存在");
			}
			row = accountMapper.updateSelective(info);
		}
		if(row == 0) {
			return BaseRespone.error("保存失败");
		}
		return BaseRespone.success("修改成功");
	}

}
